package com.zk5_education.service_edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zk5_education.R;
import com.zk5_education.service_edu.entity.EduTeacher;
import com.zk5_education.service_edu.entity.vo.TeacherQuery;
import com.zk5_education.service_edu.service.EduTeacherService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-08-01
 */
@RestController
@RequestMapping("/service_edu/teacher")
@CrossOrigin
public class EduTeacherController {
    @Autowired
    EduTeacherService teacherService;

    @GetMapping("/finAll")
    public R findAll(){
        List<EduTeacher> list = teacherService.list(null);
        return R.success().data("items",list);

    }

    @ApiOperation(value="逻辑删除")
    @DeleteMapping("{id}")
    public R DeleteTeacher(@ApiParam(name="id",value = "教师id",required = true) @PathVariable String id){
        boolean b = teacherService.removeById(id);
        return b?R.success():R.error();
    }

    /**
     * 分页查询
     * @param current 当前页
     * @param limit  每页的个数
     * @return
     */
    @GetMapping("PageTeacher/{current}/{limit}")
    public R PageTeacher(@PathVariable Long current,
                         @PathVariable Long limit){
        Page<EduTeacher> pageTeacher=new Page<>(current,limit);
        teacherService.page(pageTeacher,null);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        return R.success().data("total",total).data("rows",records);
    }

    //条件分页查询
    @PostMapping ("PageTeacherCondition/{current}/{limit}")
    public R PageTeacherCondition(@PathVariable Long current,
                                  @PathVariable Long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){

        Page<EduTeacher> pageTeacher=new Page<>(current,limit);
        QueryWrapper<EduTeacher>wrapper=new QueryWrapper<>();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        Integer level = teacherQuery.getLevel();
        String name = teacherQuery.getName();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_modified",end);
        }
        wrapper.orderByDesc("gmt_create");
        teacherService.page(pageTeacher,wrapper);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();
        return R.success().data("total",total).data("rows",records);
    }


    @PostMapping("addTeacher")
    public R AddTeacher(@RequestBody EduTeacher eduTeacher ){

        boolean save = teacherService.save(eduTeacher);
        return save?R.success():R.error();
    }

//    根据id查询教师
    @PostMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher byId = teacherService.getById(id);
        return R.success().data("teacher",byId);
    }

//    修改教师信息
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean b = teacherService.updateById(eduTeacher);
        return b?R.success():R.error();
    }
}

